package 剑指offer;

import java.util.*;


public class _32从上到下打印二叉树 {
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    public int[] levelOrder(TreeNode root) {
        if(root == null) {
            return new int[0];
        }
        List<Integer> list = new ArrayList<Integer>();
        Queue<TreeNode> queue = new PriorityQueue<>();

        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode pNode = queue.poll();
            list.add(pNode.val);
            if(pNode.left != null) {
                queue.offer(pNode.left);
            }
            if(pNode.right != null) {
                queue.offer(pNode.right);
            }
        }
        int[] ans = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ans[i] = list.get(i);
        }
        return ans;
    }
}
